package com.roc.service.transReport;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roc.bean.SnReleaseBean;
import com.roc.dao.DaoException;
import com.roc.dao.IBaseDao;
import com.roc.dao.ISQLBaseDao;
import com.roc.newPage.IPageDao;
import com.roc.pojo.Operators;
import com.roc.service.ServiceException;
import com.roc.util.constant.SysLogType;
import com.roc.web.action.terminal.SnReleaseSearchCondition;
@Service
@Transactional
public class SnReleaseServiceImpl implements  SnReleaseService{
	private Logger logger=LogManager.getLogger(SnReleaseServiceImpl.class);
	@Autowired
	private IPageDao pageDao;
	@Autowired
	private ISQLBaseDao sqlBaseDao;
	@Autowired
	private IBaseDao baseDao;
	@Override
	public boolean SnRelease(Operators op, JSONObject json) throws ServiceException  {
		// TODO Auto-generated method stub
		logger.info("开始释放SN！！！");
		try {
		String updSql = "UPDATE tbl_term_inf t SET t.device_cd= 'YJ00'||device_cd_sequence.nextval,T.REC_OPR_ID='U',T.REC_UPD_TS=CAST(SYSDATE AS TIMESTAMP),t.STATUS='02' "
				+ "  WHERE T.DEVICE_CD  =? AND t.mcht_cd=?  AND T.TERM_ID=? ";
		String delSql1 ="DELETE FROM yj_pos_info  t WHERE t.serial_num=? ";
		String delSql2 =" DELETE FROM yj_pos_agent  t WHERE t.serial_num=? ";
		String delSql3 =" DELETE FROM yj_sn_info  t WHERE t.new_sn=? " ;
		
		String delfreSql = "DELETE FROM  apstfr.tbl_tfr_mchnt_freeze_list s  WHERE s.resver4=?  AND s.freeze_had_sum='0' " ;
		
		String dealSql="   DELETE  FROM apsmgm.tbl_MCHT_BIZ_DEAL T WHERE T.MCHT_CD= ?  AND t.prod_cd='1171' ";
		String dealApstfr = " DELETE  FROM apstfr.tbl_MCHT_BIZ_DEAL T WHERE T.MCHT_CD= ?  AND t.prod_cd='1171' " ;
		String FeeSql =" DELETE  FROM apsmgm.TBL_MCHT_BIZ_FEE  t WHERE  t.mcht_cd=?  AND t.prod_cd='1171'  " ;
		
		List<Object> par=new ArrayList<Object>();
		JSONArray arr = (JSONArray) json.get("data");
		for (int i = 0; i < arr.size(); i++) {
			JSONObject param = (JSONObject) arr.get(i);
			par.add(param.get("did"));   //终端序列号
			par.add(param.get("mid"));   //商户号
			par.add(param.get("tid"));   //终端编号
			
			String ss = (String) param.get("did");
			List<Object> delpar=new ArrayList<Object>();
			delpar.add(ss);
			//查询group_cd 状态  为3 时需要判断是否有强扣记录
			String sql  = " SELECT t.group_cd FROM tbl_mcht_inf  t WHERE  mcht_cd= ? ";
			Object[] parmas={param.get("mid")};
			List<Object> list = sqlBaseDao.queryAll(sql, parmas);
			//查询是否没有发生强扣
		    String querySql = "SELECT COUNT(1)  FROM  apstfr.tbl_tfr_mchnt_freeze_list s  WHERE s.resver4=?  AND s.freeze_had_sum='0' ";
			List<Object>  queryPar = new ArrayList<Object>();
			queryPar.add(param.get("did"));
			int count = pageDao.countByParams(querySql, queryPar); 
			String sn=ss.substring(0, 2);
			if(("3".equals(list.get(0))&&count>0)){
				    sqlBaseDao.deleteBySql(delfreSql, delpar);   //删除强扣关系apstfr.tbl_tfr_mchnt_freeze_list
			        sqlBaseDao.deleteBySql(updSql, par);    //修改终端表tbl_term_inf
					sqlBaseDao.deleteBySql(delSql1, delpar); //删除yj_pos_info
					sqlBaseDao.deleteBySql(delSql2, delpar);// 删除yj_pos_agent
				    sqlBaseDao.deleteBySql(delSql3, delpar);  //删除yj_sn_info
				    
				    String mchtCd = (String) param.get("mid");
					List<Object> mchtParm=new ArrayList<Object>();
					mchtParm.add(mchtCd);
				    sqlBaseDao.deleteBySql(dealSql, mchtParm);  //删除产品表中T0的数据
				    sqlBaseDao.deleteBySql(dealApstfr, mchtParm);  //删除apstfr库中T0 产品信息
				    sqlBaseDao.deleteBySql(FeeSql, mchtParm);  //删除费率表中T0的数据
				    mchtParm.clear();
				    
			}else if("3".equals(list.get(0))&&count==0&&!"YJ".equals(sn)){
				 return false;
			}else{
				    sqlBaseDao.deleteBySql(updSql, par);    
					sqlBaseDao.deleteBySql(delSql1, delpar); 
					sqlBaseDao.deleteBySql(delSql2, delpar);
					sqlBaseDao.deleteBySql(delSql3, delpar);  
			}
				logger.info("Sn【"+param.get("did")+"】释放成功!");
				baseDao.addSyslog(SysLogType.SN_RELEASE.getCode(), SysLogType.SN_RELEASE.getName(), op.getAccountNo(), "操作员【"+op.getAccountNo()+"】"+"释放"+"Sn【"+param.get("did")+"】成功!");
				par.clear();
				delpar.clear();
				parmas=null;
				queryPar.clear();
		}	
			} catch (DaoException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new ServiceException("SN释放失败");
			}
		
		return true;
		
	}
	@Override
	public int  NoSnRelease(Operators op,SnReleaseSearchCondition searchCondition) {
		List<Object> parmas=new ArrayList<Object>();
		String delSql1 ="DELETE FROM yj_pos_info  t WHERE t.serial_num=? ";
		String delSql2 =" DELETE FROM yj_pos_agent  t WHERE t.serial_num=? ";
		String delSql3 =" DELETE FROM yj_sn_info  t WHERE t.new_sn=? " ;
		parmas.add(searchCondition.getDeviceCd());	
		try {
			if(searchCondition.getDeviceCd()!=null&&!"".equals(searchCondition.getDeviceCd())){
			sqlBaseDao.deleteBySql(delSql1, parmas);
			sqlBaseDao.deleteBySql(delSql2, parmas);
			sqlBaseDao.deleteBySql(delSql3, parmas);
			baseDao.addSyslog(SysLogType.SN_RELEASE.getCode(), SysLogType.SN_RELEASE.getName(), op.getAccountNo(), "操作员【"+op.getAccountNo()+"】"+"释放"+"Sn【"+searchCondition.getDeviceCd()+"】成功!");
			logger.info("Sn【"+searchCondition.getDeviceCd()+"】释放成功!");
			}
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.info("Sn【"+searchCondition.getDeviceCd()+"】释放失败!");
			return 0;
		}
		return 1;
		
		
	}



}
